package kd.xkqj.unittest.plugin.action;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.psi.PsiFile;
import kd.xkqj.unittest.plugin.CodeGenerator;
import kd.xkqj.unittest.plugin.JavaFileParser;
import org.jetbrains.annotations.NotNull;

import java.io.File;

import static kd.xkqj.unittest.plugin.action.AnActionHelper.*;
import static kd.xkqj.unittest.plugin.model.JavaFileInfo.*;

/**
 * @description: TODO
 * @author feng_jun_peng
 * @create 2024-12-18 19:35
 * @version 1.0
 */
public class EditorRightClickAction extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // 在这里实现按钮点击后的操作
        Project project = e.getProject();
        if (project == null) {
            Messages.showErrorDialog("找不到项目！", "Error");
            return;
        }

        PsiFile currentFile = e.getData(PlatformDataKeys.PSI_FILE);
        if (currentFile == null) {
            Messages.showErrorDialog("没有选择任何文件！", "Error");
            return;
        }

        // 获取当前编辑器
        Editor editor = e.getData(com.intellij.openapi.actionSystem.CommonDataKeys.EDITOR);
        if (editor == null) {
            Messages.showMessageDialog("找不到编辑器！", "Error", Messages.getErrorIcon());
            return;
        }

        var sourcePath = getCurrentFilePath(e);
        var targetPath = getTestClassFilePath(e);
        if (targetPath == null) {
            return;
        }

        var sourceJavaFileInfo = JavaFileParser.parseJavaFile(sourcePath);
        var cursorLine = getEditorCursorLine(e);
        var hitMethodName = sourceJavaFileInfo.getMethodNameByLineNumber(cursorLine);
        if (hitMethodName == null) {
            return;
        }
        var newTestMethodName = "test" + hitMethodName.substring(0, 1).toUpperCase() + hitMethodName.substring(1);

        var targetJavaFileInfo = JavaFileParser.parseJavaFile(targetPath);

        boolean exist = isExistTargetMethod(targetJavaFileInfo.getPublicMethodInfos(), newTestMethodName) ||
                isExistTargetMethod(targetJavaFileInfo.getProtectedMethodInfos(), newTestMethodName) ||
                isExistTargetMethod(targetJavaFileInfo.getPrivateMethodInfos(), newTestMethodName);

        if (exist) {
            var startLine = targetJavaFileInfo.getLineNumberByMethodName(newTestMethodName);
            reloadFileFromDisk(project, targetPath, startLine);
        } else {
            //不存在方法，在最后追加
            var lastLine = targetJavaFileInfo.getTotalLine();
            CodeGenerator.completeOneTestMethod(project, targetPath, hitMethodName);
            reloadFileFromDisk(project, targetPath, lastLine);
        }
    }


    private String getTestClassFilePath(AnActionEvent e) {
        var targetPath = getCurrentFilePath(e).replace("\\src\\main", "\\src\\test").replace(".java", "Test.java");
        if (!new File(targetPath).exists()) {
            Messages.showErrorDialog("没有找到对应路径的单元测试类文件：" + targetPath, "Error");
            return null;
        }
        return targetPath;
    }
}
